package com.soonfor.evaluate.tools;

import org.json.JSONException;
import org.json.JSONObject;

import com.soonfor.evaluate.base.EvalHeadBean;
import com.soonfor.evaluate.base.PageTurnEvalBean;

public class JsonAnalysisUtils {

    /**
     * 解析json数据的头部
     *
     * @return
     */
    public static EvalHeadBean getHeadBean(String jsonStr) {
        EvalHeadBean bean = new EvalHeadBean();
        if (jsonStr != null && !jsonStr.equals("")) {
            try {
                JSONObject json = new JSONObject(jsonStr);
                bean.setMsgcode(json.getInt("msgcode"));
                bean.setMsg(json.getString("msg"));
                bean.setData(json.getString("data"));
                bean.setSuccess(json.getBoolean("success"));
            } catch (Exception e) {
                if (e != null) {
                    if (e.getMessage().equals("No value for success")) {

                    } else {
                        e.printStackTrace();
                    }
                }
            }
        }
        return bean;
    }

    /**
     * 处理解析出的json数据的头部
     *
     * @return 用于任何类
     */
    public static void analysisJsonHead(String jsonStr, OperateData operateDataInfance) {
        EvalHeadBean bean = getHeadBean(jsonStr);
        if (bean!=null && bean.getMsgcode() == 0) {
            if(bean.getData()!=null) operateDataInfance.doingInSuccess(bean.getData());
        } else {
            operateDataInfance.backInFailure(bean.getFaileMsg());
        }
    }
    /**
     * 处理解析出的json数据的页码相关部分
     *
     * @return
     */
    public static PageTurnEvalBean getPageBean(String jsonStr) {
        PageTurnEvalBean bean = new PageTurnEvalBean();
        if (jsonStr != null && !jsonStr.equals("")) {
            try {
                JSONObject json = new JSONObject(jsonStr);
                bean.setRowCount(json.getInt("rowCount"));
                bean.setPageNo(json.getInt("pageNo"));
                bean.setPageSize(json.getInt("pageSize"));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return bean;
    }

    public interface OperateData {
        void doingInSuccess(String data);

        void backInFailure(String data);
    }

}
